/**
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 * $Author: skahl $
 * $Revision: 1.7 $
 * $Date: 2007/12/05 22:28:06 $
 * (c)Copyright 2004
 * education.au limited
 * DEST
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the names education.au limited, DEST nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * To the extent permitted by law, the copyright owners of this software and
 * its contributors:
 * (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising
 * in any way from the use of the software whether arising from or in
 * relation to breach of contract, negligence or any other tort, in equity
 * or otherwise. If the software is in breach of a warranty which is
 * implied by law, the copyright owners and contributors liability is
 * limited to the replacement of the software.
 *
 * @author	gsingh
 */

package au.edu.educationau.opensource.dsm.worker;

import org.apache.log4j.Logger;

import au.edu.educationau.opensource.dsm.adapters.SearchAdapter;

/**
 * A SearchTask is responsible for controlling the SearchAdapter instance it is
 * provided. It is triggered off by the SearchJob.
 * 
 * @see au.edu.educationau.opensource.dsm.worker.SearchJob
 */

public class SearchTask extends java.lang.Object implements java.lang.Runnable {
	static Logger logger = Logger.getLogger(SearchTask.class.getName());

	private SearchAdapter adapter = null;
	private long timeTaken = 0;

	/**
	 * Constructor
	 * 
	 * @param adapter
	 */
	public SearchTask(SearchAdapter adapter) {
		this.adapter = adapter;
	}

	/** Returns the status of this task */
	public short getStatus() {
		return adapter.getStatus();
	}

	/** Returns the number of results found */
	public int getResultCount() {
		return adapter.getTotalFound();
	}

	/** Returns the number of results found - Alternate naming convention */
	public int getTotalFound() {
		return adapter.getTotalFound();
	}

	/** Returns the time taken to execute this task */
	public synchronized long getTimeTaken() {
		return timeTaken;
	}

	/** Returns the SRW Diagnostic code if any */
	public String getDiagnosticCode() {
		return adapter.getDiagnosticCode();
	}

	/** Terminate this task by telling the adapter to finish up IMMEDIATELY */
	public void terminate() {
		this.adapter.terminate();
	}

	/**
	 * Takes the timestamp before and after firing the adapter's state machine
	 * for timing calculations
	 */
	public void run() {
		if (null != adapter) {
			long time1 = System.currentTimeMillis();
			adapter.fireStateMachine();
			long time2 = System.currentTimeMillis();
			synchronized (this) {
				this.timeTaken = time2 - time1;
			}
			adapter.calculateTimeTaken(timeTaken);
		}
		logger.info("ADAPTER " + adapter.getSearchCriteria().getToken() + " " + toString() + " " + getTotalFound() + " " + (getTimeTaken() / 1000f) + " " + getStatus() + " " + getDiagnosticCode());
	}

	/** Class name display */
	public String toString() {
		return adapter.getProperties().getAdapterCode();
	}

}
